package com.ljkj.zjf.rest;

import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Map;

import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ljkj.access.Tuser;
import com.ljkj.common.application.SpringApplicationContext;
import com.ljkj.common.db.domain.ZjfGoodsClass;
import com.ljkj.common.db.domain.ZjfGoodsType;
import com.ljkj.common.db.util.JSONUTIL;
import com.ljkj.common.db.util.UUID;
import com.ljkj.zjf.common.CacheManager;
import com.ljkj.zjf.common.GlobalNames;
import com.ljkj.zjf.service.TuserService;
import com.ljkj.zjf.service.ZjfGoodsClassService;
import com.ljkj.zjf.service.ZjfGoodsTypeService;

@Path("rest/goodsClass")
public class ZjfGoodsClassSource {
	
	private static ZjfGoodsClassService zjfGoodsClassService;
	private static TuserService tuserService;
	
	//初始化service
	static {
		zjfGoodsClassService  = (ZjfGoodsClassService) SpringApplicationContext.getBean("zjfGoodsClassService");
		tuserService =  (TuserService) SpringApplicationContext.getBean("tuserService");
	}
	
	private static Logger logger = LoggerFactory.getLogger(ZjfGoodsClassSource.class);
	
	@Path("/list")
	@POST
	@Produces("application/json; charset=utf-8")
	public String goodsClassList(String json_para) throws Exception {
		
		String decode_str = URLDecoder.decode(json_para, "utf-8");
		decode_str = JSONUTIL.JsonFromString(decode_str);
		JSONObject json_object = JSON.parseObject(decode_str);
		String userId = json_object.getString("userId"); 
		
		Map<String, Object> errorMap = new HashMap<String, Object>();
		errorMap.put("errCode", GlobalNames.CALL_RESULT_FAIL);
		
		if (userId == null || "".equals(userId)) {
			errorMap.put("errMsg", "用户名不能为空");
			return JSON.toJSONString(errorMap);
		}
		
		String matchRes = CacheManager.getSession().getIfPresent(userId);
		if (null == matchRes || "".equals(matchRes)) {
			errorMap.put("errMsg", "您尚未登录，请登录再操作。");
			return JSON.toJSONString(errorMap);
		}
		
		String params = json_object.getString("params");
		String pageIndex = json_object.getString("pageIndex");
		int currentPage = 1;
		if(pageIndex != null && !"".equals(pageIndex)){
			currentPage = Integer.parseInt(pageIndex)+1;
		}
		String pageSize_str = json_object.getString("pageSize");
		int pageSize = 10;
		if(pageSize_str != null && !"".equals(pageSize_str)){
			pageSize = Integer.parseInt(pageSize_str);
		}
		String sortField = json_object.getString("sortField");
		String sortOrder = json_object.getString("sortOrder");
		
		String result = this.zjfGoodsClassService.getGoodsClassByProperties(params, currentPage, pageSize, sortField, sortOrder);
		return result;
		
	}
	
	@Path("/list_for_son")
	@GET
	@Produces("application/json; charset=utf-8")
	public String goodsClassListForClass(String json_para) throws Exception {
		
		String result = this.zjfGoodsClassService.getGoodsClassForSonClass();
		return result;
	}
	
	
	@Path("/info")
	@POST
	@Produces("application/json; charset=utf-8")
	public String addOrEditGoodsClassInfo(String json_para) throws Exception {
		
		String decode_str = URLDecoder.decode(json_para, "utf-8");
		decode_str = JSONUTIL.JsonFromString(decode_str);
		JSONObject json_object = JSON.parseObject(decode_str);
		
		Map<String, Object> errorMap = new HashMap<String, Object>();
		errorMap.put("errCode", GlobalNames.CALL_RESULT_FAIL);
		String userId = json_object.getString("userId"); 
		if (userId == null || "".equals(userId)) {
			errorMap.put("errMsg", "用户名不能为空");
			return JSON.toJSONString(errorMap);
		}
		
		String classId = json_object.getString("classId");
		ZjfGoodsClass zjfGoodsClass = null;
		if(classId != null && !"".equals(classId)){
			zjfGoodsClass = this.zjfGoodsClassService.getGoodsClassById(classId);
		}
		if(zjfGoodsClass == null){
			zjfGoodsClass = new ZjfGoodsClass();
		}
		
		String className = json_object.getString("className");
		if(className!= null){
			zjfGoodsClass.setClassName(className);
		}
		String classTypeId = json_object.getString("classTypeId");
		if(classTypeId != null && !"".equals(classTypeId)){
			zjfGoodsClass.setClassTypeId(classTypeId);
		}
		
		String classLevel_temp = json_object.getString("classLevel");
		Integer classLevel = null;
		if(classLevel_temp != null && !"".equals(classLevel_temp)){
			classLevel = Integer.parseInt(classLevel_temp);
			zjfGoodsClass.setClassLevel(classLevel);
		}
		
		String classIcon = json_object.getString("classIcon");
		if(classIcon != null && !"".equals(classIcon)){
			zjfGoodsClass.setClassIcon(classIcon);
		}
		
		
		String classRecommend_temp = json_object.getString("classRecommend");
		Integer classRecommend = null;
		if(classRecommend_temp != null && !"".equals(classRecommend_temp)){
			classRecommend = Integer.parseInt(classRecommend_temp);
			zjfGoodsClass.setClassRecommend(classRecommend);
		}
		
		Integer disabledState = null;
		String disabledState_temp = json_object.getString("disabledState");
		if(disabledState_temp != null && !"".equals(disabledState_temp)){
			disabledState = Integer.parseInt(disabledState_temp);
			zjfGoodsClass.setDisabledState(disabledState);
		}
		
		Integer classSequence = null;
		String classSequence_temp = json_object.getString("classSequence");
		if(classSequence_temp != null && !"".equals(classSequence_temp)){
			classSequence = Integer.parseInt(classSequence_temp);
			zjfGoodsClass.setClassSequence(classSequence);
		}
		
		String classParentId = json_object.getString("classParentId");
		if(classParentId != null && !"".equals(classParentId)){
			zjfGoodsClass.setClassParentId(classParentId);
		}
		
		Tuser tuser = this.tuserService.getUserById(Integer.valueOf(userId));
		
		String result = this.zjfGoodsClassService.addOrEditGoodsClassInfo(zjfGoodsClass, tuser);
		return result;
		
	}


}
